package com.jihui.security.web.userdetails;

import com.jihui.user.model.JihuiUser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.Date;

import static javax.swing.text.StyleConstants.ModelAttribute;

/**
 * controller advice:  get current login account
 *
 * @author linux_china
 */
@ControllerAdvice
public class CurrentAccountControllerAdvice {
    @Value("${info.account.logout.url}")
    private String logoutUrl;

    @Value("${info.account.login.url}")
    private String loginUrl;

    @Value("${info.domain.version}")
    private String version;

    @ModelAttribute("currentAccount")
    public JihuiUser getAccountAccount(Authentication authentication) {
        return (authentication == null) ? null : ((CurrentUserDetails) authentication.getPrincipal());
    }

    @ModelAttribute("logoutUrl")
    public String getLogoutUrl() {
        return logoutUrl;
    }

    @ModelAttribute("loginUrl")
    public String getLoginUrl() {
        return loginUrl;
    }

    @ModelAttribute("version")
    public String getVersion() {
        return version + "?t=" + new Date().getTime();
    }
}
